package memento.main.originator;

import memento.main.memento.Memento;

import java.util.Random;

/**
 * 挑战者
 */
public class Hero {
    //血液值
    private int blood;

    //武力值
    private int sword;

    private final Random random = new Random();

    public Hero() {
        this.blood = 100;
        this.sword = 100;
    }

    //创建备忘录保存内容
    public Memento createMemento() {
        System.out.println("创建备忘录...");
        return new Memento(this.blood, this.sword);
    }

    //恢复备忘录内容
    public void restoreFromMemento(Memento memento) {
        System.out.println("恢复备忘录...");
        if (memento != null) {
            this.blood = memento.getBlood();
            this.sword = memento.getSword();
        }
    }

    /**
     * 挑战boss
     */
    public int koBoss() {
        //当血液值<=0时，挑战失败，假设展示BOSS的概率为2%
        //判断时还有血液值
        if (this.blood <= 0 || this.sword <= 0) {
            System.out.println(this.toString());
            System.out.println("挑战Boss失败！");
            return -1;
        } else {
            //获取随机数
            double win = Math.random();
            System.out.println(this.toString());
            if (win <= 0.02) {
                System.out.println("恭喜你，挑战BOSS成功！");
                return 1;
            } else {
                System.out.println("继续攻击Boss...");
                //随机数，减少血液值和武力值，继续KO
                int blood_sub = random.nextInt(10);
                int sword_sub = random.nextInt(10);
                this.blood -= blood_sub;
                this.sword -= sword_sub;
                return 0;
            }
        }
    }

    public String toString() {
        return String.format("当前血液值：%d - 当前武力值：%d ", this.blood, this.sword);
    }
}
